package com.yuanye.gwcit.fragments;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;

import com.yuanye.gwcit.GW;
import com.yuanye.yeecho.base.CommonTaskFragment;
import com.yuanye.yeecho.utils.BlueToothController;
import com.yuanye.yeecho.utils.YY;

public class BluetoothFragment extends CommonTaskFragment {

    BlueToothController controller;
    BluetoothReceiver bluetoothReceiver;
    IntentFilter filter;
    public BluetoothFragment() {
        setTaskName("蓝牙测试");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        controller = new BlueToothController();
        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
    }

    @Override
    public void execTask() {
        super.execTask();
        String std_bluetoothModel = GW.cfgPcba.getBluetoothModel();
        String bluetoothModel = YY.execShell("getprop persist.vendor.wifi_name");
        appendLog("蓝牙型号:"+bluetoothModel);
        if (std_bluetoothModel.equals(bluetoothModel)){
            appendLog("型号比对成功\n");
        }else{
            appendLogF("型号比对失败，参考值："+std_bluetoothModel);
            setFail();
            return;
        }

//        runInBackground();
        if (controller.isDiscovering()){
            appendLog("已处于蓝牙搜索状态，取消搜索");
            controller.cancelSearch();
        }
        bluetoothReceiver = new BluetoothReceiver();
        context.registerReceiver(bluetoothReceiver, filter);
        controller.findDevice();
    }

    @Override
    public void stopTask() {
        super.stopTask();
        if (bluetoothReceiver != null){
            context.unregisterReceiver(bluetoothReceiver);
        }
    }

    private int bluetoothCount = 0;
    class BluetoothReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
                appendLog("开启蓝牙搜索..");
            }else if(intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
                bluetoothCount++;
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String info = "";
                String name = "未知设备";
                if (device.getName() != null)
                    name = device.getName();
                if (device.getBondState() == 12) {
                    info = "设备名：" + name + "\n" + "设备地址：" + device.getAddress() + "\n" + "连接状态：已配对" + "\n";
                }
                else if (device.getBondState() == 10){
                    info = "设备名：" + name + "\n" + "设备地址：" + device.getAddress() + "\n" + "连接状态：未配对" +"\n";
                }else{
                    info = "设备名：" + name + "\n" + "设备地址：" + device.getAddress() + "\n" + "连接状态：未知" + "\n";
                }
                appendLog(info);
                controller.cancelSearch();
//                setPass();
            }else if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                //处理测试结果
                appendLog("蓝牙搜索结束 -- 共发现"+bluetoothCount+"个蓝牙设备");
                if (bluetoothCount >= 1){
//                    setPass();
                    passAndNext();
                }else{
                    appendLogF("测试失败，未发现足够个数的蓝牙设备");
                    setFail();

                }
            }
        }
    }
}
